package cn.myfirefly.moming.common.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;

import cn.myfirefly.moming.common.dao.DataDictDao;
import cn.myfirefly.moming.common.entity.MMDataDict;
import cn.myfirefly.moming.common.model.DataDictDto;
import cn.myfirefly.moming.common.model.DataDictFilterDto;
import cn.myfirefly.core.model.PageResultDto;
import cn.myfirefly.core.model.SelectItemDto;

@Service
@Transactional
public class DataDictService {
	
	@Autowired
	private DataDictDao dataDictDao;
	
	public PageResultDto<DataDictDto> getList(DataDictFilterDto dto) {
		
		return dataDictDao.getList(dto);
	}
	
	public List<SelectItemDto> getListByDataType(String dataType) {
		List<MMDataDict> dataList = dataDictDao.getListByDataType(dataType);
		List<SelectItemDto> dtoList = null;
		if (dataList != null) {
			dtoList = new ArrayList<SelectItemDto>();
			SelectItemDto selectItemDto = null;
			for (Iterator<MMDataDict> iterator = dataList.iterator(); iterator.hasNext();) {
				MMDataDict dataDict = iterator.next();
				selectItemDto = new SelectItemDto();
				selectItemDto.setLable(dataDict.getDataShowName());
				selectItemDto.setValue(dataDict.getDataValue());
				dtoList.add(selectItemDto);
			}
		}
		return dtoList;
	}
	
	public List<JSONObject> getListByDataTypeForJson(String dataType) {
		List<MMDataDict> dataList = dataDictDao.getListByDataType(dataType);
		List<JSONObject> dtoList = new ArrayList<JSONObject>();
		if (dataList != null) {
			JSONObject obj = new JSONObject();
			for (Iterator<MMDataDict> iterator = dataList.iterator(); iterator.hasNext();) {
				MMDataDict dataDict = iterator.next();
				obj.put(dataDict.getDataValue(), dataDict.getDataShowName());
			}
			dtoList.add(obj);
		}
		return dtoList;
	}
}
